/**
 * Copyright 2014-2015 www.fsfkj.com
 * All rights reserved.
 * 
 * @project
 * @author jipengfei
 * @version 1.0
 * @date 2015-09-11
 */
package com.eyaomall.wxmall.common.util;

import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * excel读取工具类
 * 
 * @author jackey
 *
 */
public class ExcelReader {

	private static Logger LOG = LoggerFactory.getLogger(ExcelReader.class);

	public static Workbook createWorkbook(String filePath) {

		try {
			if (filePath.trim().toLowerCase().endsWith("xls")) {

				return new HSSFWorkbook(new FileInputStream(filePath));
			} else if (filePath.trim().toLowerCase().endsWith("xlsx")) {

				return new XSSFWorkbook(new FileInputStream(filePath));
			} else {
				throw new IllegalArgumentException("不支持除：xls/xlsx以外的文件格式!!!");
			}
			
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
		
		return null;
	}
	
	/**
	 * 获取工作簿
	 * @param wb
	 * @param index
	 * @return
	 */
	public static Sheet getSheet(Workbook wb,int index){
		
		return wb.getSheetAt(index);
	}
	
	/**
	 * 获取工作簿
	 * @param wb
	 * @param index
	 * @return
	 */
	public static Sheet getSheet(Workbook wb,String sheetName){
		
		return wb.getSheet(sheetName);
	}
	
	/**
	 * 根据行号索引获取这一行的数据
	 * @param sheet
	 * @param rowIndex
	 * @return
	 */
	public static Row getRow(Sheet sheet,int rowIndex){
		
		return sheet.getRow(rowIndex);
	}
}
